﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;

public partial class Web_ListQuestions : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FetchKind();

            ///
            // 初始化
            ///
            CmsDataContext db = new CmsDataContext();
            var train = (from aT in db.Train
                         where (aT.Started - DateTime.Now).Days >= 0
                         orderby aT.Registed descending
                         select new
                         {
                             ID = aT.ID,
                             Memo = string.Format("{0}将于{1:yyyy-MM-dd HH:mm}在{2}举办", aT.Name, aT.Started, aT.Address)
                         })
                             .Take(6);
            TrainRepeater.DataSource = train;
            TrainRepeater.DataBind();


            var px2 = (from aA in db.Article where aA.Verified != null orderby aA.Verified descending select aA).Take(6);
            Repeater3.DataSource = px2;
            Repeater3.DataBind();
        }
    }
    private void BindData()
    {
        CmsDataContext db = new CmsDataContext();
        var q = from aQ in db.Question
                select aQ;

        #region 加入课程类型条件判断
        if (Kind.SelectedValue != "")
        {
            q = q.Where(aQ => aQ.Kind == Kind.SelectedValue);
        }
        if (Subject.SelectedValue != "")
        {
            q = q.Where(aQ => aQ.Subject == Subject.SelectedValue);
        }
        if (Course.SelectedValue != "")
        {
            q = q.Where(aQ => aQ.Course == Course.SelectedValue);
        }
        #endregion

        var q1 = q;
        #region 加入查询条件
        string search = SearchCondition.Text.Trim();
        if (search != "")
        {
            Expression<Func<Question, bool>> lmbda = PredicateBuilder<Question>.CreatePredicate(new string[] { 
                "Title"}, search, "aQ");
            q = q.Where(lmbda);
        }
        #endregion

        RecordView.DataSource = q1.OrderByDescending(aQ => aQ.Time).Select(aQ => new
        {
            ID = aQ.ID,
            Name = aQ.Name,
            Time = string.Format("{0:yyyy-MM-dd HH:mm}", aQ.Time),
            Title = aQ.Title,
            Kind = aQ.Kind,
            Subject = aQ.Subject,
            Course = aQ.Course
        }).ToList();
        
        RecordView.DataKeyNames = new string[] { "ID" };
        RecordView.DataBind();
    }
    private void FetchKind() {
        Kind.Items.Clear();

        XmlDataSource xds = new XmlDataSource();
        xds.DataFile = Toolkit.GetRealDataFilePath("Course.xml");
        xds.XPath = "/Root/Kind";

        Kind.DataSource = xds;
        Kind.DataTextField = "name";
        Kind.DataValueField = "name";
        Kind.DataBind();

        Kind.Items.Insert(0,new ListItem("请选择", ""));

        Subject.Items.Clear();
        Subject.Items.Insert(0, new ListItem("请选择", ""));

        Course.Items.Clear();
        Course.Items.Insert(0, new ListItem("请选择", ""));
    }
    private void FetchSubject()
    {
        Subject.Items.Clear();

        if (Kind.SelectedValue != "") {
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Course.xml");
            xds.XPath = string.Format("/Root/Kind[@name='{0}']/Subject", Kind.SelectedValue);

            Subject.DataSource = xds;
            Subject.DataTextField = "name";
            Subject.DataValueField = "name";
            Subject.DataBind();
        }

        Subject.Items.Insert(0, new ListItem("请选择", ""));

        Course.Items.Clear();
        Course.Items.Insert(0, new ListItem("请选择", ""));
    }
    private void FetchCourse()
    {
        Course.Items.Clear();

        if (Subject.SelectedValue != "")
        {
            XmlDataSource xds = new XmlDataSource();
            xds.DataFile = Toolkit.GetRealDataFilePath("Course.xml");
            xds.XPath = string.Format("/Root/Kind[@name='{0}']/Subject[@name='{1}']/Course", Kind.SelectedValue, Subject.SelectedValue);

            Course.DataSource = xds;
            Course.DataTextField = "name";
            Course.DataValueField = "name";
            Course.DataBind();
        }

        Course.Items.Insert(0, new ListItem("请选择", ""));
    }
    protected void Kind_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchSubject();
        BindData();
    }
    protected void Subject_SelectedIndexChanged(object sender, EventArgs e)
    {
        FetchCourse();
        BindData();
    }
    protected void Course_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void Search_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        BindData();
    }
}